<?php require_once("header.php"); ?>

        <div id="content">
            <div id="content-title">
                Form Posting
            </div>
            
<?php
	if(!isset($_SESSION['login_username']))
		header("Location:error_user.php");
	else {
            $formvalid = false;
            $errors['title'] = 0;
            $errors['link'] = 0;
            $errors['linkdesc'] = 0;
            $errors['imagefile'] = 0;
            $errors['filetype'] = 0;
            $errors['videourl'] = 0;
            $errors['tags'] = 0;
            
            if(isset($_POST['postbutton'])){
                $errors['title'] = $_POST['title']=='';
                if($_POST['postingtype']=='link'){                    
                    $errors['link'] = $_POST['link']=='';
                    $errors['linkdesc'] = $_POST['linkdesc']=='';
                } else if($_POST['postingtype']=='image'){
                    $errors['imagefile'] = !isset($_FILES['imagefile']) || $_FILES['imagefile']['type']=='';                    
                    $errors['filetype'] = isset($_FILES['imagefile']) && $_FILES['imagefile']['type']!='image/jpeg';
                } else if($_POST['postingtype']=='video'){
                    $errors['videourl'] = $_POST['videourl']=='';
                }
                
                $tags = explode(',', $_POST['tags']);
                $numofvalidtags = 0;
                for ($i=0; $i<count($tags); $i++){
                    $tags[$i] = trim($tags[$i]);
                    if($tags[$i]!=''){
                        $numofvalidtags++;
                    }
                }
                $errors['tags'] = $numofvalidtags==0;               
                
                if($_POST['postingtype']=='link'){                    
                    $formvalid = !$errors['title'] && !$errors['link'] && !$errors['linkdesc'] && !$errors['tags'];
                } else if($_POST['postingtype']=='image'){
                    $formvalid = !$errors['title'] && !$errors['imagefile'] && !$errors['filetype'] && !$errors['tags'];
                } else if($_POST['postingtype']=='video'){
                    $formvalid = !$errors['title'] && !$errors['videourl'] && !$errors['tags'];
                }
                
                if($formvalid){
                    require_once('db.php');
                    $url = '';
                    $desc = '';
                    if($_POST['postingtype']=='link'){                    
                        $url=$_POST['link'];
                        $desc=$_POST['linkdesc'];
                    } else if($_POST['postingtype']=='image'){
                        $target = "img/"; 
                        $target = $target . basename( $_FILES['imagefile']['name']);
                        $pic=($_FILES['imagefile']['name']);
                        move_uploaded_file($_FILES['imagefile']['tmp_name'], $target);
                        $url=$pic;
                    } else if($_POST['postingtype']=='video'){
                        $url=$_POST['videourl'];
                    }
                                        
                    if(connect_db()){
                            $sql = "INSERT INTO konten VALUES('','".$_SESSION['login_username']."','".$_POST['title']."','".date('Y:m:j')."','".date('h:i:s')."','','".$_POST['postingtype']."','".$url."','','".$desc."');";
                            echo $sql."<br />";
                            if (!mysql_query($sql))
                                    printf('A database error occurred in processing your '.
                                    'submission.\nIf this error persists, please '.
                                    'contact admin@pobunesia.org');
                            else{
                                    echo "File posted!<br />";
                            }
                    }
                }
            }
            
?>
            <form id="posting" action="" method="post" enctype="multipart/form-data">
                <ul>
                    <li>
                        <div class="prompt">Title</div><input type="text" id="title" name="title" value="<?php if(isset($_POST['title'])) echo $_POST['title']?>"/>
                        <?php if($errors['title']){ ?>
                        <div class="error_msg">Do not leave title blank!</div>
                        <?php } ?>
                    </li>
                    <li><input type="radio" name="postingtype" value="link" onclick="enableLink()" <?php if(!isset($_POST['postingtype']) || $_POST['postingtype']=='link') {?>checked="checked"<?php } ?>/>Link</li>
                    <li>
                        <div class="prompt">URL</div><input type="text" id="link" name="link"  value="<?php if(isset($_POST['postingtype']) && $_POST['postingtype']=='link' && isset($_POST['link'])) echo $_POST['link'];?>" <?php if(isset($_POST['postingtype']) && $_POST['postingtype']!='link'){?>disabled="disabled"<?php } ?>/>
                        <?php if($errors['link']){ ?>
                        <div class="error_msg">Do not leave url blank!</div>
                        <?php } ?>
                    </li>
                    <li>
                        <div class="prompt">Descriptions</div><textarea id="linkdesc" name="linkdesc" rows="3" cols="40" <?php if(isset($_POST['postingtype']) && $_POST['postingtype']!='link'){?>disabled="disabled"<?php } ?>><?php if(isset($_POST['postingtype']) && $_POST['postingtype']=='link' && isset($_POST['linkdesc'])) echo $_POST['linkdesc'];?></textarea>
                        <?php if($errors['linkdesc']){ ?>
                        <div class="error_msg">Do not leave link description blank!</div>
                        <?php } ?>
                    </li>
                    <li><input type="radio" name="postingtype" value="image" onclick="javascript:enableImage()" <?php if(isset($_POST['postingtype']) && $_POST['postingtype']=='image') {?>checked="checked"<?php } ?>/>Image<br/></li>
                    <li>
                        <div class="prompt">Please select your image file</div><input type="file" name="imagefile" id="imagefile" accept="image/jpeg" <?php if(!isset($_POST['postingtype']) || isset($_POST['postingtype']) && $_POST['postingtype']!='image'){?>disabled="disabled"<?php } ?>/>
                        <?php if($errors['imagefile']){ ?>
                        <div class="error_msg">Choose an image!</div>
                        <?php } ?>
                        <?php if($errors['filetype']){ ?>
                        <div class="error_msg">Image must be in jpg/jpeg format!</div>
                        <?php } ?>
                    </li>
                    <li><input type="radio" name="postingtype" value="video" onclick="javascript:enableVideo()" <?php if(isset($_POST['postingtype']) && $_POST['postingtype']=='video') {?>checked="checked"<?php } ?>/>Video</li>
                    <li>
                        <div class="prompt">URL (from Youtube)</div><input type="text" name="videourl" id="videourl" value="<?php if(isset($_POST['postingtype']) && $_POST['postingtype']=='video' && isset($_POST['videourl'])) echo $_POST['title'];?>" <?php if(!isset($_POST['postingtype']) || isset($_POST['postingtype']) && $_POST['postingtype']!='video'){?>disabled="disabled"<?php } ?>/>
                        <?php if($errors['videourl']){ ?>
                        <div class="error_msg">Do not leave video url blank!</div>
                        <?php } ?>
                    </li>
                    <li>
                        <div class="prompt">Tags</div><input type="text" name="tags" id="tags" placeholder="tag1, tag2, tag3" value="<?php if(isset($_POST['tags'])) echo $_POST['tags']?>"/>
                        <?php if($errors['tags']){ ?>
                        <div class="error_msg">No valid tag! Please insert tags separated by commas.</div>
                        <?php } ?>
                    </li>
                    <li><div id="preview">Preview</div></li>
                    <li><div class="prompt"><input class="actionbutton" type="button" value="Preview" name="postbutton" onclick="showPreview();"/></div>
                    <div class="prompt"><input class="actionbutton" type="submit" value="Post" name="postbutton"/></div></li>
                </ul>
            </form>
        </div>
<?php require_once("footer.php"); } ?>
